﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.AdminWeb.DtoModels;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Framework.MvcPager;
using MvcMall.Core.Enums;

namespace MvcMall.AdminWeb.Manage.StoreApplies
{
    public partial class AppliesList : ManageBasePage
    {
        private IStoreApplyTask _storeApplyTask;


        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("SelectAppliesList");
            _storeApplyTask = GetInstance<IStoreApplyTask>();

            if (!IsPostBack)
            {
                BindRpt();
            }
        }

        private void BindRpt()
        {
            int ddlSelect = -1;
            int.TryParse(ddlState.SelectedValue, out ddlSelect);

            var list = _storeApplyTask.GetPagedList(ANPager.CurrentPageIndex, ANPager.PageSize, txtUserName.Text.Trim(), ddlSelect==-1?(StoreApplyStatus?) null:ddlSelect==1?StoreApplyStatus.待审核:ddlSelect==2?StoreApplyStatus.审核通过:StoreApplyStatus.审核不通过);
            var dtoList = list.Select(c => new StoreApplyDto
            {
                Id = c.Id,
                CreateTime = c.CreateTime,
                IdentityCardId = c.IdentityCardId,
                IdentityCardImage = c.IdentityCardImage,
                LicenseImage = c.LicenseImage,
                OwnerName = c.OwnerName,
                PromoteUserName =
                    c.PromoteUser == null ? string.Empty : c.PromoteUser.UserName,
                StoreApplyStatus = c.StoreApplyStatus,
                StoreCatName = c.StoreCat.CatName,
                StoreGradeName = c.StoreGrade.GradeName,
                StoreName = c.StoreName,
                StoreType = c.StoreType,
                UserName = c.User.UserName
            }).ToPagedList(ANPager.CurrentPageIndex, ANPager.PageSize,
                                                              list.TotalItemCount);
            rptRoles.DataSource = dtoList;
            rptRoles.DataBind();
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindRpt();
        }

        protected void ANPager_PageChanged(object sender, EventArgs e)
        {
            BindRpt();
        }
    }
}